home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 21 / Cream of the Crop 21 (Terry Blount) (October 1996).iso / os2 / e33el2.zip / emacs / 19.33 / lisp / scribe.el < prev    next >
Lisp/Scheme  |  1996-01-20  |  12KB  |  326 lines

  1. ;;; scribe.el --- scribe mode, and its idiosyncratic commands.
  2.  
  3. ;; Copyright (C) 1985 Free Software Foundation, Inc.
  4.  
  5. ;; Maintainer: FSF
  6. ;; Keywords: wp
  7.  
  8. ;; This file is part of GNU Emacs.
  9.  
  10. ;; GNU Emacs is free software; you can redistribute it and/or modify
  11. ;; it under the terms of the GNU General Public License as published by
  12. ;; the Free Software Foundation; either version 2, or (at your option)
  13. ;; any later version.
  14.  
  15. ;; GNU Emacs is distributed in the hope that it will be useful,
  16. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18. ;; GNU General Public License for more details.
  19.  
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs; see the file COPYING.  If not, write to the
  22. ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  23. ;; Boston, MA 02111-1307, USA.
  24.  
  25. ;;; Commentary:
  26.  
  27. ;; A major mode for editing source in written for the Scribe text formatter.
  28. ;; Knows about Scribe syntax and standard layout rules.  The command to
  29. ;; run Scribe on a buffer is bogus; someone interested should fix it.
  30.  
  31. ;;; Code:
  32.  
  33. (defvar scribe-mode-syntax-table nil
  34.   "Syntax table used while in scribe mode.")
  35.  
  36. (defvar scribe-mode-abbrev-table nil
  37.   "Abbrev table used while in scribe mode.")
  38.  
  39. (defvar scribe-fancy-paragraphs nil
  40.   "*Non-NIL makes Scribe mode use a different style of paragraph separation.")
  41.  
  42. (defvar scribe-electric-quote nil
  43.   "*Non-NIL makes insert of double quote use `` or '' depending on context.")
  44.  
  45. (defvar scribe-electric-parenthesis nil
  46.   "*Non-NIL makes parenthesis char ( (]}> ) automatically insert its close
  47. if typed after an @Command form.")
  48.  
  49. (defconst scribe-open-parentheses "[({<"
  50.   "Open parenthesis characters for Scribe.")
  51.  
  52. (defconst scribe-close-parentheses "])}>"
  53.   "Close parenthesis characters for Scribe.
  54. These should match up with `scribe-open-parenthesis'.")
  55.  
  56. (if (null scribe-mode-syntax-table)
  57.     (let ((st (syntax-table)))
  58.       (unwind-protect
  59.        (progn
  60.     (setq scribe-mode-syntax-table (copy-syntax-table
  61.                     text-mode-syntax-table))
  62.     (set-syntax-table scribe-mode-syntax-table)
  63.     (modify-syntax-entry ?\" "    ")
  64.     (modify-syntax-entry ?\\ "    ")
  65.     (modify-syntax-entry ?@ "w   ")
  66.     (modify-syntax-entry ?< "(>  ")
  67.     (modify-syntax-entry ?> ")<  ")
  68.     (modify-syntax-entry ?[ "(]  ")
  69.     (modify-syntax-entry ?] ")[  ")
  70.     (modify-syntax-entry ?{ "(}  ")
  71.     (modify-syntax-entry ?} "){  ")
  72.     (modify-syntax-entry ?' "w   "))
  73.        (set-syntax-table st))))
  74.  
  75. (defvar scribe-mode-map nil)
  76.  
  77. (if scribe-mode-map
  78.     nil
  79.   (setq scribe-mode-map (make-sparse-keymap))
  80.   (define-key scribe-mode-map "\t" 'scribe-tab)
  81.   (define-key scribe-mode-map "\e\t" 'tab-to-tab-stop)
  82.   (define-key scribe-mode-map "\es" 'center-line)
  83.   (define-key scribe-mode-map "\e}" 'up-list)
  84.   (define-key scribe-mode-map "\eS" 'center-paragraph)
  85.   (define-key scribe-mode-map "\"" 'scribe-insert-quote)
  86.   (define-key scribe-mode-map "(" 'scribe-parenthesis)
  87.   (define-key scribe-mode-map "[" 'scribe-parenthesis)
  88.   (define-key scribe-mode-map "{" 'scribe-parenthesis)
  89.   (define-key scribe-mode-map "<" 'scribe-parenthesis)
  90.   (define-key scribe-mode-map "\C-c\C-c" 'scribe-chapter)
  91.   (define-key scribe-mode-map "\C-c\C-t" 'scribe-section)
  92.   (define-key scribe-mode-map "\C-c\C-s" 'scribe-subsection)
  93.   (define-key scribe-mode-map "\C-c\C-v" 'scribe-insert-environment)
  94.   (define-key scribe-mode-map "\C-c\C-e" 'scribe-bracket-region-be)
  95.   (define-key scribe-mode-map "\C-c[" 'scribe-begin)
  96.   (define-key scribe-mode-map "\C-c]" 'scribe-end)
  97.   (define-key scribe-mode-map "\C-c\C-i" 'scribe-italicize-word)
  98.   (define-key scribe-mode-map "\C-c\C-b" 'scribe-bold-word)
  99.   (define-key scribe-mode-map "\C-c\C-u" 'scribe-underline-word))
  100.  
  101. ;;;###autoload
  102. (defun scribe-mode ()
  103.   "Major mode for editing files of Scribe (a text formatter) source.
  104. Scribe-mode is similar text-mode, with a few extra commands added.
  105. \\{scribe-mode-map}
  106.  
  107. Interesting variables:
  108.  
  109. scribe-fancy-paragraphs
  110.   Non-nil makes Scribe mode use a different style of paragraph separation.
  111.  
  112. scribe-electric-quote
  113.   Non-nil makes insert of double quote use `` or '' depending on context.
  114.  
  115. scribe-electric-parenthesis
  116.   Non-nil makes an open-parenthesis char (one of `([<{')
  117.   automatically insert its close if typed after an @Command form."
  118.   (interactive)
  119.   (kill-all-local-variables)
  120.   (use-local-map scribe-mode-map)
  121.   (setq mode-name "Scribe")
  122.   (setq major-mode 'scribe-mode)
  123.   (define-abbrev-table 'scribe-mode-abbrev-table ())
  124.   (setq local-abbrev-table scribe-mode-abbrev-table)
  125.   (make-local-variable 'comment-start)
  126.   (setq comment-start "@Comment[")
  127.   (make-local-variable 'comment-start-skip)
  128.   (setq comment-start-skip (concat "@Comment[" scribe-open-parentheses "]"))
  129.   (make-local-variable 'comment-column)
  130.   (setq comment-column 0)
  131.   (make-local-variable 'comment-end)
  132.   (setq comment-end "]")
  133.   (make-local-variable 'paragraph-start)
  134.   (setq paragraph-start (concat "\\([\n\f]\\)\\|\\(@\\w+["
  135.                  scribe-open-parentheses
  136.                 "].*["
  137.                  scribe-close-parentheses
  138.                 "]$\\)"))
  139.   (make-local-variable 'paragraph-separate)
  140.   (setq paragraph-separate (if scribe-fancy-paragraphs
  141.                    paragraph-start "$"))
  142.   (make-local-variable 'sentence-end)
  143.   (setq sentence-end "\\([.?!]\\|@:\\)[]\"')}]*\\($\\| $\\|\t\\|  \\)[ \t\n]*")
  144.   (make-local-variable 'compile-command)
  145.   (setq compile-command (concat "scribe " (buffer-file-name)))
  146.   (set-syntax-table scribe-mode-syntax-table)
  147.   (run-hooks 'text-mode-hook 'scribe-mode-hook))
  148.  
  149. (defun scribe-tab ()
  150.   (interactive)
  151.   (insert "@\\"))
  152.  
  153. ;; This algorithm could probably be improved somewhat.
  154. ;;  Right now, it loses seriously...
  155.  
  156. (defun scribe ()
  157.   "Run Scribe on the current buffer."
  158.   (interactive)
  159.   (call-interactively 'compile))
  160.  
  161. (defun scribe-envelop-word (string count)
  162.   "Surround current word with Scribe construct @STRING[...].
  163. COUNT specifies how many words to surround.  A negative count means
  164. to skip backward."
  165.   (let ((spos (point)) (epos (point)) (ccoun 0) noparens)
  166.     (if (not (zerop count))
  167.     (progn (if (= (char-syntax (preceding-char)) ?w)
  168.            (forward-sexp (min -1 count)))
  169.            (setq spos (point))
  170.            (if (looking-at (concat "@\\w[" scribe-open-parentheses "]"))
  171.            (forward-char 2)
  172.          (goto-char epos)
  173.          (skip-chars-backward "\\W")
  174.          (forward-char -1))
  175.            (forward-sexp (max count 1))
  176.            (setq epos (point))))
  177.     (goto-char spos)
  178.     (while (and (< ccoun (length scribe-open-parentheses))
  179.         (save-excursion
  180.           (or (search-forward (char-to-string
  181.                        (aref scribe-open-parentheses ccoun))
  182.                       epos t)
  183.               (search-forward (char-to-string
  184.                        (aref scribe-close-parentheses ccoun))
  185.                       epos t)))
  186.         (setq ccoun (1+ ccoun))))
  187.     (if (>= ccoun (length scribe-open-parentheses))
  188.     (progn (goto-char epos)
  189.            (insert "@end(" string ")")
  190.            (goto-char spos)
  191.            (insert "@begin(" string ")"))
  192.       (goto-char epos)
  193.       (insert (aref scribe-close-parentheses ccoun))
  194.       (goto-char spos)
  195.       (insert "@" string (aref scribe-open-parentheses ccoun))
  196.       (goto-char epos)
  197.       (forward-char 3)
  198.       (skip-chars-forward scribe-close-parentheses))))
  199.  
  200. (defun scribe-underline-word (count)
  201.   "Underline COUNT words around point by means of Scribe constructs."
  202.   (interactive "p")
  203.   (scribe-envelop-word "u" count))
  204.  
  205. (defun scribe-bold-word (count)
  206.   "Boldface COUNT words around point by means of Scribe constructs."
  207.   (interactive "p")
  208.   (scribe-envelop-word "b" count))
  209.  
  210. (defun scribe-italicize-word (count)
  211.   "Italicize COUNT words around point by means of Scribe constructs."
  212.   (interactive "p")
  213.   (scribe-envelop-word "i" count))
  214.  
  215. (defun scribe-begin ()
  216.   (interactive)
  217.   (insert "\n")
  218.   (forward-char -1)
  219.   (scribe-envelop-word "Begin" 0)
  220.   (re-search-forward (concat "[" scribe-open-parentheses "]")))
  221.  
  222. (defun scribe-end ()
  223.   (interactive)
  224.   (insert "\n")
  225.   (forward-char -1)
  226.   (scribe-envelop-word "End" 0)
  227.   (re-se